﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Sapiens.at.SharePoint.Settings;
using Microsoft.SharePoint;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using Sapiens.at.SharePoint.Extensions;
using System.Data;
using System.Data.SqlClient;
using Sapiens.at.SharePoint.Extensions;
using Sapiens.at.SharePoint.Utils;
using QB = Sapiens.at.SharePoint.Utils.QueryBuilder;
using System.Threading;

namespace Sapiens.at.SharePoint.SQLM.CONTROLTEMPLATES.Sapiens.at.SharePoint.SQLM
{
    public partial class SQLMUpdateItemsSection : SettingsSection
    {

        public string CTypeID { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            (ifsUpdateItems as InputFormSection).Title = SPContext.Current.Web.GetLocalizedString("SQLMifsUpdateItems");
            ifsUpdateItemsDesc.Text = SPContext.Current.Web.GetLocalizedString("SQLMifsUpdateItemsDesc");
            cbUpdateItems.Text = SPContext.Current.Web.GetLocalizedString("SQLMUpdateAllItemsOnSave");
        }

        public override string UserControlPath
        {
            get { return "~/_CONTROLTEMPLATES/" + SP2013.Path + "Sapiens.at.SharePoint.SQLM/SQLMUpdateItemsSection" + SP2013.NameEmptyOr15 + ".ascx"; }
        }
        public override void Save()
        {
            Page.Validate();
            if (Page.IsValid)
            {
                if (cbUpdateItems.Checked)
                {
                    using (SPLongOperation operation = new SPLongOperation(Page))
                    {
                        try
                        {
                            operation.LeadingHTML = SPContext.Current.Web.GetLocalizedString("SQLMUpdatingAllItemsNow");//"Updating all items.";
                            operation.TrailingHTML = SPContext.Current.Web.GetLocalizedString("SQLMUpdatingWillTakeAWhile");//"This can take several minutes.";
                            operation.Begin();
                            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                            {
                                using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                                {
                                    
                                    foreach (SPListItem i in web.Lists[SPContext.Current.List.ID].GetItems(new SPQuery().Where(QB.IsContentType(new SPContentTypeId(CTypeID))).Select("ID").Scope(SPViewScope.RecursiveAll)).Cast<SPListItem>().ToArray())
                                    {
                                        i.SystemUpdate(false);
                                    }
                                }
                            }
                            operation.End(this.FindParent<CONTROLTEMPLATES.Sapiens.at.SharePoint.SQLM.Settings>().PageToRedirectOnCancel);
                        }
                        catch (ThreadAbortException ex2) { }
                        catch (Exception ex)
                        {
                            Microsoft.SharePoint.Utilities.SPUtility.TransferToErrorPage(ex.Message);
                        }
                    }
                }
            }
        }
    }
    
}
